<!--
 * @Author: 大国男儿 7824741+lc2677@user.noreply.gitee.com
 * @Date: 2022-10-12 20:28:02
 * @LastEditors: 大国男儿 7824741+lc2677@user.noreply.gitee.com
 * @LastEditTime: 2022-10-12 21:52:10
 * @FilePath: \vue-event\src\views\user\userPwd.vue
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
-->
<template>
  <el-card class="card">
    <div slot="header" class="div_text">
      <span>重置密码</span>
    </div>
    <!-- 表单 -->
    <el-form :model="pwdForm" :rules="pwdFormRules" ref="pwdFormRef" label-width="100px">
      <el-form-item label="原密码" prop="old_pwd">
        <el-input v-model="pwdForm.old_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="new_pwd">
        <el-input v-model="pwdForm.new_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item label="确认新密码" prop="re_pwd">
        <el-input v-model="pwdForm.re_pwd" type="password"></el-input>
      </el-form-item>
      <el-form-item class="el-form-item__content">
        <el-button type="primary" @click="updatePwd">修改密码</el-button>
        <el-button @click="reset">重置</el-button>
      </el-form-item>
    </el-form>
  </el-card>
</template>

<script>
import { updatePwdAPI } from '@/api'
export default {
  name: 'UserPwd',
  data () {
    // 新密码和旧密码不能一样
    const samePwd = (rules, value, callback) => {
    // value就是校验规则所对应输入框值
      if (value === this.pwdForm.old_pwd) {
        callback(new Error('新旧密码不能相同！'))
      } else {
        callback()// 通过校验
      }
    }
    // 两次密码保持一致
    const rePwd = (rules, value, callback) => {
      if (value !== this.pwdForm.new_pwd) {
        callback(new Error('输入两次密码不一致！'))
      } else {
        callback()
      }
    }
    return {
      // 表单的数据对象
      pwdForm: {
        old_pwd: '',
        new_pwd: '',
        re_pwd: ''
      },
      // 表单的验证规则对象
      pwdFormRules: {
        old_pwd: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' }
        ],
        new_pwd: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' },
          { validator: samePwd, trigger: 'blur' }
        ],
        re_pwd: [
          { required: true, message: '请再次确认新密码', trigger: 'blur' },
          { pattern: /^\S{6,15}$/, message: '密码长度必须是6-15位的非空字符串', trigger: 'blur' },
          { validator: rePwd, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    updatePwd () {
      this.$refs.pwdFormRef.validate(async valid => {
        if (valid) {
          const res = await updatePwdAPI(this.pwdForm)
          console.log(res.data)
          if (res.data.code !== 0) {
            this.$message.error('原密码不正确')
          } else {
            this.$message.success('更新密码成功')
            this.$refs.pwdFormRef.resetFields()
            // 需要重新登录
            this.$store.commit('updateToken', '') // 清空 token
            this.$store.commit('updateUserInfo', {})// 清空 userInfo
            this.$router.push('/login')
          }
        } else {
          return false
        }
      })
    },
    // 重置
    reset () {
      this.$refs.pwdFormRef.resetFields()
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 500px;
}
.card{
  width: 600px
}
.div_text{
  text-align:center
}
.el-form-item__content{
  margin-left: 100px;
}
</style>
